package com.huichenghe.xinlvsh01.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huichenghe.xinlvsh01.DataEntites.OutLineDataEntity;
import com.huichenghe.xinlvsh01.R;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * 适配器，将离线数据分条目展现在RecyclerView
 * 此类继承自RecyclerView的内部类Adapter
 * Created by lixiaoning on 15-11-17.
 */
public class MainPagerRecyclerAdapter extends RecyclerView.Adapter<MainPagerRecyclerAdapter.MyRecyclerViewHolder>
{
    private Context mContext;
    private ArrayList<OutLineDataEntity> dataList;
    // 引用点击借口
    private MyOnItemClickListener myOnItemClickListener;




    public MainPagerRecyclerAdapter(Context mContext, ArrayList<OutLineDataEntity> list)
    {
        this.mContext = mContext;
        this.dataList = list;
    }


    /**
     * 此方法创建一个ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 加载布局，获取item布局对象
        View vItem = LayoutInflater.from(mContext).inflate(R.layout.mainpager_movement_list, parent, false);
        // 创建ViewHolder对象，并传入view
        MyRecyclerViewHolder h = new MyRecyclerViewHolder(vItem);
        return h;
    }


    /**
     * 绑定数据到holder上的各个控件
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MyRecyclerViewHolder holder, final int position) {
        holder.duration.setText(dataList.get(position).getTime());
        holder.heartrate.setText(dataList.get(position).getHeartReat() + "bmp");
        holder.calorie.setText(dataList.get(position).getCalorie() + "千卡");
        holder.step.setText(dataList.get(position).getStepCount() + "步");
        int mType = dataList.get(position).getType();
        switch (mType)
        {
            case OutLineDataEntity.TYPE_WALK:
                mType = R.drawable.walk;
                break;
            case OutLineDataEntity.TYPE_RUNNINT:
                mType = R.drawable.running;
                break;
            case OutLineDataEntity.TYPE_BALL_MOVEMENT:
                mType = R.drawable.ball;
                break;
            case OutLineDataEntity.TYPE_CLIMING:
                mType = R.drawable.climing;
                break;
            case OutLineDataEntity.TYPE_MUSCLE:
                mType = R.drawable.muscle;
                break;
            case OutLineDataEntity.TYPE_AEROBIC:
                mType = R.drawable.aerobic;
                break;
            case OutLineDataEntity.TYPE_UNKNOW:
                mType = R.drawable.unkonw_type;
                break;
        }
        Log.i("", "类型：" + mType);
        holder.icon.setImageResource(mType);

        // 设置item监听
        holder.mRelativeLayout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                myOnItemClickListener.OnClick(position);
            }
        });

    }


    /**
     * 底层获取数据条目数量
     * @return
     */
    @Override
    public int getItemCount() {
        return dataList.size();
    }


    /**
     * holer，底层只管回收holder，通过构造方法获取控件，并赋值给成员变量
     */
    class MyRecyclerViewHolder extends RecyclerView.ViewHolder
    {
        TextView duration, step, calorie, heartrate;
        ImageView icon;
        RelativeLayout mRelativeLayout;
        public MyRecyclerViewHolder(View itemView) {
            super(itemView);
            duration = (TextView)itemView.findViewById(R.id.movement_duratin_main);
            icon = (ImageView)itemView.findViewById(R.id.movement_type_main);
            step = (TextView)itemView.findViewById(R.id.movement_step_count_main);
            calorie = (TextView)itemView.findViewById(R.id.movement_kll_main);
            heartrate = (TextView)itemView.findViewById(R.id.movement_heart_reat_main);
            mRelativeLayout = (RelativeLayout)itemView.findViewById(R.id.layout_item_outline);
        }
    }



    public interface MyOnItemClickListener
    {
        public void OnClick(int position);
    }


    public void setMyOnItemClickListener(MyOnItemClickListener myOnItemClickListener)
    {
        this.myOnItemClickListener = myOnItemClickListener;
    }

}
